using System.Collections.Generic;
using System.Linq;
using System.Web.Script.Serialization;
using Orchard.ContentManagement.MetaData.Builders;

namespace DarkSky.Microdata.Settings {
    public class MicrodataPartSettings {
        private IList<MicrodataEntry> _entries;

        public string EntryDefinitions { get; set; }

        public MicrodataPartSettings() {
            EntryDefinitions = "[]";
        }

        public IList<MicrodataEntry> Entries {
            get { return _entries ?? (_entries = new JavaScriptSerializer().Deserialize<MicrodataEntry[]>(EntryDefinitions).ToList()); }
            set { 
                _entries = value;
                EntryDefinitions = new JavaScriptSerializer().Serialize(value.ToArray());
            }
        }

        public void Build(ContentTypePartDefinitionBuilder builder) {
            builder.WithSetting("MicrodataPartSettings.EntryDefinitions", EntryDefinitions);
        }
    }
}